package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * 并且需要增加菜品的口味
     * 使用需要事务控制
     * @param dishDTO
     */
    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 向菜品表新增一条数据
        dishMapper.addDish(dish);
        // 利用主键回显，获取 dishId

        // 向口味表新增多条条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!= null && !flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryDishList(DishPageQueryDTO dishPageQueryDTO) {
        // 利用pageHelper分页查询
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.queryDishList(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 删除菜品业务规则
     * 1. 起售状态的菜品不能删除
     * 2. 被套餐关联的菜品不能被删除
     * 3. 删除菜品后，关联的口味数据也要删除
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        // 判断当前菜品是否在起售中
        for (Long id : ids) {
            if (dishMapper.getStatustById(id) == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 判断当前菜品是否在套餐中
        List<Long> setmealIds = setmealDishMapper.getSetmealByIds(ids);
        if (setmealIds!= null && !setmealIds.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除菜品
        dishMapper.deleteDish(ids);

        // 删除菜品口味
        dishFlavorMapper.deleteDishFlavor(ids);
    }

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @Override
    public DishVO getDishById(Long id) {
        // 先获取菜品基本属性
        Dish dish = dishMapper.getDishById(id);

        // 在获取菜品口味信息
        List<DishFlavor> dishFlavors = dishFlavorMapper.getDishFlavorsByDishId(id);

        // 将上面两种信息封装到 DishVO 对象中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Transactional
    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);

        // 修改菜品基本属性
        dishMapper.updateDish(dish);

        // 先删除原先的口味信息
        dishFlavorMapper.deleteDishFlavorsByDishId(dishDTO.getId());

        // 再重新增加新的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors!= null && !flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dish.getId());
            }
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public void setDishStatus(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.updateDish(dish);
    }

    @Override
    public List<Dish> queryDishByCategoryId(Long categoryId) {
        return dishMapper.queryDishByCategoryId(categoryId);
    }
}
